package com.yan.yjhTest.tryTest;

/**
 * try-catch-finally执行顺序
 */
public class TryTest {

    /**
     * try中有return时
     * @param x
     * @param y
     * @return
     */
    public static int test1(int x, int y){
        int result = x;
        try{
            if (x<0){
                return 0;
            }
            result =x+y;
            return result;
        }finally {
            result = x - y;
        }
    }

    /**
     * try和finally中有return时，程序执行到finally时会提前返回
     * @param x
     * @param y
     * @return
     */
    public static int test2(int x, int y){
        int result = x;
        try{
            if (x<0){
                return 0;
            }
            result =x+y;
            return result;
        }finally {
            result = x - y;
            return result;
        }
    }

    public static void main(String[] args) {
        int x= 3;
        int y =5;
        int test1Result = test1(x,y);//输出8
        int test2Result = test2(x,y);//输出-2
        System.out.println(test1Result);
        System.out.println(test2Result);
    }
}
